package com.src.java.collection.queue;

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Random;

public class PriorityQueueExample {
	public static void main(String[] args) {
        
        //natural ordering example of priority queue
        Queue<Integer> integerPriorityQueue = new PriorityQueue<Integer>(7);
        Random rand = new Random();
        for(int i=0;i<7;i++){
        	int randValue = rand.nextInt(100);
        	System.out.println("Generated Integer Value:" + randValue);
            integerPriorityQueue.add(new Integer(randValue));
        }
        for(int i=0;i<7;i++){
            Integer in = integerPriorityQueue.poll();
            System.out.println("Processing Integer:"+in);
        }
         
        //PriorityQueue example with Comparator
        Queue<Customer> customerPriorityQueue = new PriorityQueue<Customer>(7, idComparator);
        addDataToQueue(customerPriorityQueue);
         
        pollDataFromQueue(customerPriorityQueue);
         
    }

	// Comparator nested class implementation
	public static Comparator<Customer> idComparator = new Comparator<Customer>() {

		@Override
		public int compare(Customer c1, Customer c2) {
			return (int) (c1.getId() - c2.getId());
		}
	};

	// utility method to add random data to Queue
	private static void addDataToQueue(Queue<Customer> customerPriorityQueue) {
		Random rand = new Random();
		for (int i = 0; i < 7; i++) {
			int id = rand.nextInt(100);
			System.out.println("Generated Customer Id:" + id);
			customerPriorityQueue.add(new Customer(id, "Pankaj " + id));
		}
	}

	// utility method to poll data from queue
	private static void pollDataFromQueue(Queue<Customer> customerPriorityQueue) {
		while (true) {
			Customer cust = customerPriorityQueue.poll();
			if (cust == null) {
				break;
			}
			System.out.println("Processing Customer with ID=" + cust.getId());
		}
	}
}
